//package com.tianji.exam.service.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.tianji.api.client.learning.LearningClient;
//import com.tianji.common.exceptions.BizIllegalException;
//import com.tianji.common.utils.BeanUtils;
//import com.tianji.common.utils.CollUtils;
//import com.tianji.exam.domain.ExamType;
//import com.tianji.exam.domain.dto.ExamDTO;
//import com.tianji.exam.domain.po.ExamRecord;
//import com.tianji.exam.domain.po.Question;
//import com.tianji.exam.domain.po.QuestionBiz;
//import com.tianji.exam.domain.vo.ExamVO;
//import com.tianji.exam.mapper.QuestionBizMapper;
//import com.tianji.exam.mapper.QuestionMapper;
//import com.tianji.exam.service.IExamService;
//import com.tianji.exam.utils.ExamRepository;
//import lombok.RequiredArgsConstructor;
//import org.bson.Document;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Example;
//import org.springframework.data.domain.ExampleMatcher;
//import org.springframework.stereotype.Service;
//
//import java.time.LocalDateTime;
//import java.time.LocalTime;
//import java.util.List;
//import java.util.Set;
//import java.util.stream.Collectors;
//
///**
// * @author 86152
// * @date 2023/08/28
// */
//@Service
//public class ExamServiceImpl implements IExamService {
//
//    private final ExamRepository examRepository;
//
//    @Autowired
//    private QuestionBizMapper questionBizMapper;
//    @Autowired
//    private QuestionMapper questionMapper;
//    @Autowired
//    private LearningClient learningClient;
//
//
//    @Autowired
//    public ExamServiceImpl(ExamRepository examRepository) {
//        this.examRepository = examRepository;
//    }
//
//    @Override
//    public ExamVO queryExamAndBegin(ExamDTO examDTO) {
//        //判断课程是否购买
//        Long courseId = examDTO.getCourseId();
//        if (courseId == null) {
//            throw new BizIllegalException("课程不存在");
//        }
//       /* Long lessonValid = learningClient.isLessonValid(courseId);
//        if (lessonValid == null) {
//            throw new BizIllegalException("课程未购买");
//        }*/
//        Long sectionId = examDTO.getSectionId();
//        if (sectionId == null) {
//            return null;
//        }
//        //查询考试记录
//        ExamRecord examRecord = new ExamRecord();
//        examRecord.setSectionId(examDTO.getSectionId());
//        Example<ExamRecord> example = Example.of(examRecord);
//        boolean exists = examRepository.exists(example);
//        if (exists) {
//            if (examDTO.getType() == ExamType.EXAM) {
//                throw new BizIllegalException("不能重复考试");
//            }
//        }
//        LambdaQueryWrapper<QuestionBiz> eq = new LambdaQueryWrapper<QuestionBiz>().eq(QuestionBiz::getBizId, sectionId);
//        List<QuestionBiz> questionBizs = questionBizMapper.selectList(eq);
//        Set<Long> questionIds = questionBizs.stream().map(QuestionBiz::getQuestionId).collect(Collectors.toSet());
//        LambdaQueryWrapper<Question> in = new LambdaQueryWrapper<Question>().in(Question::getId, questionIds);
//        List<Question> questions = questionMapper.selectList(in);
//        if (CollUtils.isEmpty(questions)) {
//            throw new BizIllegalException("问题不存在");
//        }
//        examRecord.setCourseId(examDTO.getCourseId());
//        examRecord.setBeginTime(LocalDateTime.now());
//        examRecord.setType(examDTO.getType().getValue());
//        ExamRecord record = examRepository.save(examRecord);
//        ExamVO examVO = new ExamVO();
//        examVO.setQuestions(questions);
//        examVO.setId(record.getId());
//        return examVO;
//    }
//}
